#!/bin/bash

if [ $# = 1 ]
then
    configScript=$1
else
    echo No configure script path passed in!!!
    exit
fi
if [ -f $configScript ]
then
    source $configScript
else
    echo Invalid configure script path!!!
    exit
fi

implScript=InitLinuxImpl.sh
if [ -f $implScript ]
then
    source $implScript
else
    echo No $implScript in current folder!!!
    exit
fi

##################
echo Now use ROOT!

### Check proxy config ###
isProxyNeeded=$(GetConfigVar IsConfigureProxy $OS $VER)
AssertFunctionResult
if [ $isProxyNeeded = true ]
then
    ConfigureProxy
fi

### Add users ###
flag=$(GetConfigVar IsCreateUsers $OS $VER)
AssertFunctionResult
if [ $flag = true ]
then
    newUsers=($(GetConfigArray NewUsers $OS $VAR))
    for((i=0; i<${#newUsers[@]}; i++))
    do
        AddUser ${newUsers[${i}]}
    done
fi

### Add FTP user ###
flag=$(GetConfigVar IsCreateFTPUser $OS $VER)
AssertFunctionResult
if [ $flag = true ]
then
    ftpUser=$(GetConfigVar FTPUser $OS $VER)
    AssertFunctionResult
    AddUser $ftpUser
fi

### Install utilities ###
flag=$(GetConfigVar IsInstallUtilities $OS $VER)
AssertFunctionResult
if [ $flag = true ]
then
    if [ $isProxyNeeded = true ]
    then
        read -p "Proxy is required for this system, so please make sure it is setup and then press any key to continue ..." tmp
    fi

    utilities=($(GetConfigArray Utilities $OS $VAR))
    AssertFunctionResult
    installApp=$(GetConfigVar InstallApp $OS $VER)
    AssertFunctionResult

    UpdateInstallAppSource $installApp

    for((i=0; i<${#utilities[@]}; i++))
    do
        InstallUtil $installApp ${utilities[${i}]}
    done
fi

### Configure vsftp ###
flag=$(GetConfigVar IsConfigureFTP $OS $VER)
AssertFunctionResult
if [ $flag = true ]
then
    newUsers=($(GetConfigArray NewUsers $OS $VAR))
    AssertFunctionResult

    ConfigureFTP ${newUsers[@]}
fi

### Configure emacs ###
flag=$(GetConfigVar IsConfigureEmacs $OS $VER)
AssertFunctionResult
if [ $flag = true ]
then
    ConfigureEmacs
fi

echo Now exit ROOT!
